

var DotNetNuke = {
	//A generic extend function - thanks MooTools
	extend: function (original, extended) {
		for (var key in (extended || {})) {
		  if (original.hasOwnProperty(key)) {
			original[key] = extended[key];
		  }
		}
		return original;
	},
	
	createPicker: function(options){
		var defaults = {
			top: 'auto',
			left: 'auto',
			right: 'auto',
			bottom: 'auto',
			width: 'auto',
			height: 'auto',
			backgroundColor: '#fff',
			hintText: '',
			color: '#000',
			editable: false,
			borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
			borderColor: '#777',
			borderWidth: 2,
			data: []
		};
		
		//There are some settings we don't want the user to control
		var noOverride = {
			editable: false
		};
		
		var pickerOptions = DotNetNuke.extend(defaults, options);
		pickerOptions = DotNetNuke.extend(pickerOptions, noOverride);
		
		var _picker = Ti.UI.createView({
			height: pickerOptions.height,
			width: pickerOptions.width,
			top: pickerOptions.top,
			left: pickerOptions.left,
			right: pickerOptions.right,
			backgroundColor: pickerOptions.backgroundColor
		});
				
		if (pickerOptions.borderStyle == Ti.UI.INPUT_BORDERSTYLE_ROUNDED){
			_picker.borderRadius=5;
			_picker.borderWidth=pickerOptions.borderWidth;
			_picker.borderColor=pickerOptions.borderColor;
		};
		
		var _textbox = Ti.UI.createLabel({
			top: 2,
			left: 7,
			right: pickerOptions.height + 10,
			bottom: 2,
			backgroundColor: pickerOptions.backgroundColor,
			color: pickerOptions.color,
			text: pickerOptions.hintText
		});
		
		_picker.addEventListener('setText', function(data){
			_textbox.text = data.text;
		});
		
		var _dropdown = Ti.UI.createView({
			right: 4,
			width: pickerOptions.height - 8,
			height: pickerOptions.height - 8,
			backgroundImage: 'images/dropdown.png'
		});
		
		_picker.add(_textbox);
		_picker.add(_dropdown);
		
		return _picker;
		
	},
	
	createSelector: function(options) {
		var defaults = {
				top: 'auto',
				left: 'auto',
				right: 'auto',
				bottom: 'auto',
				width: 'auto',
				height: 'auto',
				backgroundColor: '#fff',
				color: '#000',
				borderColor: '#777',
				data: []
			};

		var _selectorOptions = DotNetNuke.extend(defaults, options);
		
		var selectorData = [];
		
		var _selector = Ti.UI.createScrollView({
		    contentWidth:'auto',
		    contentHeight:'auto',
		    top:_selectorOptions.top,
		    showVerticalScrollIndicator:true,
		    showHorizontalScrollIndicator:false,
			scrollType: 'vertical',
			borderRadius: 10,
			borderColor: _selectorOptions.borderColor,
			borderWidth: 2,
			backgroundColor: _selectorOptions.backgroundColor,
			width: _selectorOptions.width,
			height: _selectorOptions.height
		});

		var _table = Ti.UI.createTableView({
			top: 5,
			left: 5,
			width: _selectorOptions.width - 10,
			height: _selectorOptions.height - 10
		});

		for (var i = 0; i < _selectorOptions.data.length; i++){

			var row = Ti.UI.createTableViewRow();
			if (!_selectorOptions.data[i].selected) {
				_selectorOptions.data[i].selected=false;
			};
			
			var label = Ti.UI.createLabel({
				text:_selectorOptions.data[i].label,
				font:{fontSize:16},
				color: _selectorOptions.color,
				width:'auto',
				textAlign:'left',
				top:10,
				left:10,
				height:24
			});
			
			var selectedImage =  Ti.UI.createImageView({
				width:34,
				height:34,
				right:10
			});
			
			selectedImage.image = _selectorOptions.data[i].selected ? 'images/selected.png' : 'images/unselected.png';

			row.add(label);
			row.add(selectedImage);
			row.label=_selectorOptions.data[i].label;
			row.hasChild=false;
			row.selected=_selectorOptions.data[i].selected;
			row.className = 'search_row';

			selectorData.push(row);
		};
		
		_table.setData(selectorData);
		_selector.add(_table);
/*		_table.addEventListener('click', function(e){
			_selector.fireEvent('click', e);
		});
*/		
		return _selector;
	},
	selectDataRow: function (data, index){
		for (var i = 0; i < data.length; i++){
			data[i].selected = i==index ? true : false;
		}
	},
	getSelectedDataRow: function(data){
		var index = 0;
		for (var i = 0; i < data.length; i++){
			if(data[i].selected) {
				index = i;
			}
		}
		return data[index];
	},	
	openBrowser: function(url) {
		//need some error trapping here
		Ti.App.Properties.setString('browser_url', url);
		var browser = Ti.UI.createWindow( {
			url: 'browser.js',
			navBarHidden: true,
		    backgroundImage:DotNetNuke.getBackgroundImage()
		});
		browser.open();		
	},
	isValidURL: function(url){ 
		var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	 	return regexp.test(url);
	},
	isAndroid: function(){
		return (Ti.Platform.osname === 'android');
	},
	isLargeScreen: function(){
		return (Titanium.Platform.displayCaps.platformHeight > 500);
	},
	getBackgroundImage: function(){
		return this.isLargeScreen() ? 'images/pulse_background_blue_large.png' : 'images/pulse_background_blue.png';
	},
	getTime: function(dt){
		var a_p = '';
		var curr_hour = dt.getHours();
		
		a_p = (curr_hour < 12) ? 'AM' : 'PM';

		if (curr_hour == 0)   {
		   curr_hour = 12;
		}
		if (curr_hour > 12){
		   curr_hour = curr_hour - 12;
		}

		var curr_min = dt.getMinutes();
		if (curr_min < 10) {
			curr_min = '0' + curr_min;
		}

		return curr_hour + ':' + curr_min + ' ' + a_p;

	},
	getLongDate: function(dt){
		var d_names = ["Sunday", 
		               "Monday", 
		               "Tuesday",
		               "Wednesday", 
		               "Thursday", 
		               "Friday", 
		               "Saturday"];

		var m_names = ["January", 
		               "February", 
		               "March",
		               "April", 
		               "May", 
		               "June", 
		               "July", 
		               "August", 
		               "September",	
		               "October", 
		               "November", 
		               "December"];

		var curr_day = dt.getDay();
		var curr_date = dt.getDate();

		var sup = "";
		if (curr_date == 1 || curr_date == 21 || curr_date ==31)
		   {
		   sup = "st";
		   }
		else if (curr_date == 2 || curr_date == 22)
		   {
		   sup = "nd";
		   }
		else if (curr_date == 3 || curr_date == 23)
		   {
		   sup = "rd";
		   }
		else
		   {
		   sup = "th";
		   }
		var curr_month = dt.getMonth();
		var curr_year = dt.getFullYear();

		return d_names[curr_day] + ' ' + curr_date + sup + ' ' + m_names[curr_month] + ' ' + curr_year;
	},
	linkColor: '#49bdff'
};

DotNetNuke.favorites = {
	addFavorite: function(userGroup){
	
	},
	getFavorite: function(userGroupId){
		return null;
	},
	removeFavorite: function(userGroupId){
		
	},
	isFavorite: function(userGroupId){
		return false;
	},
	getFavorites: function() {
		return [];
	}
};